package jal4eclipse.ui.editors;

import jal4eclipse.jal.JALConstant;

import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WordRule;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

public class JALEditorScanner extends RuleBasedScanner {
	private static final Color COLOR_KEYWORD;
	private static final Color COLOR_KEYWORD2;
	private static final Color COLOR_KEYWORD3;
	private static final Color COLOR_TYPES;
	private static final Color COLOR_COMMENT;
	private static final Color COLOR_OTHER;
	private static final Color COLOR_STRING;
	private static final Color COLOR_ANNOTATION;
	private static final Color COLOR_ASM;
	
	static {
		Display d = PlatformUI.getWorkbench().getDisplay();
		COLOR_KEYWORD = new Color(d, 0, 0, 128);
		COLOR_KEYWORD2 = new Color(d, 0, 0, 200);
		COLOR_TYPES = new Color(d, 0, 0, 128);
		COLOR_KEYWORD3 = new Color(d, 255, 0, 0);
		
		
		COLOR_COMMENT = new Color(d, 0, 128, 0);
		COLOR_OTHER = new Color(d, 0, 0, 0);
		COLOR_STRING = new Color(d, 0, 0, 200);
		COLOR_ANNOTATION = new Color(d, 0, 128, 0);
		COLOR_ASM = new Color(d, 0xA0,0 , 0);
		
	}
	
	/**
	 * Initialise le scanner.
	 */
	public JALEditorScanner() {
		Token keyword = new Token(new TextAttribute(COLOR_KEYWORD, null, SWT.BOLD));
		Token keyword2 = new Token(new TextAttribute(COLOR_KEYWORD2));
		Token keyword3 = new Token(new TextAttribute(COLOR_KEYWORD3));
		Token types = new Token(new TextAttribute(COLOR_TYPES, null, SWT.BOLD));
		Token comment = new Token(new TextAttribute(COLOR_COMMENT, null, SWT.ITALIC));
		Token other = new Token(new TextAttribute(COLOR_OTHER));
		Token string = new Token(new TextAttribute(COLOR_STRING));
		Token annotation = new Token(new TextAttribute(COLOR_ANNOTATION,null,SWT.BOLD));
		Token asm = new Token(new TextAttribute(COLOR_ASM,null,SWT.BOLD));
		
		
		WordRule rule = new WordRule(new IWordDetector() {
			public boolean isWordPart(char c) {
				return Character.isJavaIdentifierPart(c);
			}
			public boolean isWordStart(char c) {
				return Character.isJavaIdentifierStart(c);
			}			
		}, other);
		
		for (int i = 0;i<JALConstant.TYPES.length;i++) {
			rule.addWord(JALConstant.TYPES[i], types);
			rule.addWord(JALConstant.TYPES[i].toUpperCase(), types);
			
		}
		for (int i = 0;i<JALConstant.ASM_KEYWORDS.length;i++) {
			rule.addWord(JALConstant.ASM_KEYWORDS[i], asm);
			rule.addWord(JALConstant.ASM_KEYWORDS[i].toUpperCase(), asm);
		}
		for (int i = 0;i<JALConstant.KEYWORDS.length;i++) {
			rule.addWord(JALConstant.KEYWORDS[i], keyword);
			rule.addWord(JALConstant.KEYWORDS[i].toUpperCase(), keyword);
		}
		for (int i = 0;i<JALConstant.KEYWORDS2.length;i++) {
			rule.addWord(JALConstant.KEYWORDS2[i], keyword2);
			rule.addWord(JALConstant.KEYWORDS2[i].toUpperCase(), keyword2);
		}
		for (int i = 0;i<JALConstant.KEYWORDS3.length;i++) {
			rule.addWord(JALConstant.KEYWORDS3[i], keyword3);
			rule.addWord(JALConstant.KEYWORDS3[i].toUpperCase(), keyword3);
		}
		
		setRules(new IRule[] {
				rule,
				new SingleLineRule("--", null, comment, (char) 0, true),
				new SingleLineRule(";@", " ", annotation, (char) 0, true),
				new SingleLineRule(";", null, comment, (char) 0, true),
				new SingleLineRule("\"","\"", string, (char) 0, true)
				
		});		
	}
}